//
//  IMPVistorView.swift
//  MySina
//
//  Created by imppppp on 16/5/22.
//  Copyright © 2016年 IMP. All rights reserved.
//

import UIKit
import SnapKit

//@objc protocol IMPVistorViewDelegate: NSObjectProtocol {
//    
//    func userWillLogin()
//    func userWillRegister()
//    
//}

class IMPVistorView: UIView {
    
//    weak var delegate: IMPVistorViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // MARK: - 外界调用改变访客视图
    func updateUI(imageName: String, title: String, isHome: Bool = false) {
        
        if isHome {
            // 开始动画
            startAnimation()
        } else {
            
            bringSubviewToFront(circleIcon)
            house.hidden = true
        }
        textLabel.text = title
        circleIcon.image = UIImage(named: imageName)
        
    }
    
    
    
    // MARK: - 圈圈的转动动画
    private func startAnimation() {
        
        let basic = CABasicAnimation(keyPath: "transform.rotation")
        // 动画时间
        basic.duration = 15
        // 动画重复次数
        basic.repeatCount = MAXFLOAT
        basic.toValue = M_PI * 2
        // 当动画结束或者视图处于非活跃状态时不移除动画
        basic.removedOnCompletion = false
        // 添加动画
        circleIcon.layer.addAnimation(basic, forKey: nil)
    }
    
    
    private func setupUI() {
        
        let color: CGFloat = 237 / 255.0
        self.backgroundColor = UIColor(red: color, green: color, blue: color, alpha: 1.0)
        
        addSubview(circleIcon)
        addSubview(backIcon)
        addSubview(house)
        addSubview(textLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        // MARK: - 添加约束
        // 圈圈的约束
        circleIcon.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-50)
        }
        
        // 遮罩的约束
        backIcon.snp_makeConstraints { (make) in
            
            make.center.equalTo(circleIcon.snp_center)
        }
        
        // 房子的约束
        house.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(circleIcon.snp_centerY)
        }
        
        // 文字的约束
        textLabel.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(circleIcon.snp_centerX)
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            
            make.height.equalTo(40)
        }
        
        // 登陆按钮约束
        loginBtn.snp_makeConstraints { (make) in
            
            make.left.equalTo(textLabel.snp_left)
            make.width.equalTo(100)
            make.top.equalTo(textLabel.snp_bottom).offset(16)
        }
        
        // 注册按钮约束
        registerBtn.snp_makeConstraints { (make) in
            
            make.right.equalTo(textLabel.snp_right)
            make.width.equalTo(100)
            make.top.equalTo(loginBtn.snp_top)
        }
        
//            loginBtn.addTarget(self, action: #selector(loginBtnClick), forControlEvents: .TouchUpInside)
        
//            registerBtn.addTarget(self, action: #selector(registerBtnClick), forControlEvents: .TouchUpInside)

        
    }
    
    
    
    
    
    
    // MARK: - 懒加载
    // 圈圈
    private lazy var circleIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    // 遮罩
    private lazy var backIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    // 房子
    private lazy var house: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    // 文字
    private lazy var textLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "关注一些人，回到这里看看有什么惊喜"
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        label.font = UIFont.systemFontOfSize(14)
        label.textAlignment = .Center
        label.preferredMaxLayoutWidth = 230
        
        return label
    }()
    
    // 登录按钮
     lazy var loginBtn: UIButton = {
       
        let btn = UIButton()
        
        btn.setTitle("登陆", forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        
        return btn
    }()
    
    // 注册按钮
     lazy var registerBtn: UIButton = {
        
        let btn = UIButton()
        
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        return btn
    }()
    
//    @objc private func loginBtnClick() {
//        
//        delegate?.userWillLogin()
//    }
//    
//    @objc private func registerBtnClick() {
//        
//        delegate?.userWillRegister()
//    }
    
}
